using System.Threading.Tasks;
using Admin.NET.Application.AttributeInfo;
using Admin.NET.Application.Entity.Business;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Admin.NET.Application;

/// <summary>
/// 预警规则服务
/// </summary>
[ApiDescriptionSettings(new string[] { "业务应用" }, Order = 100)]
public class WarningRuleService : IDynamicApiController, ITransient, IPrivateDependency
{
	private readonly SqlSugarRepository<WarningRule> _rep;

	public WarningRuleService(SqlSugarRepository<WarningRule> rep)
	{
		_rep = rep;
	}

	/// <summary>
	/// 分页查询预警规则
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpGet("/warningRule/page")]
	public async Task<dynamic> Page([FromQuery] WarningRuleInput input)
	{
		ISugarQueryable<WarningRule> query = _rep.Context.Queryable<WarningRule>().WhereIF(!string.IsNullOrWhiteSpace(input.Name), (WarningRule u) => u.Name.Contains(input.Name.Trim())).WhereIF(!string.IsNullOrWhiteSpace(input.Description), (WarningRule u) => u.Description.Contains(input.Description.Trim()));
		query = query.OrderBuilder(input);
		return await query.ToPagedListAsync(input.Page, input.PageSize);
	}

	/// <summary>
	/// 增加预警规则
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/warningRule/add")]
	public async Task Add(AddWarningRuleInput input)
	{
		WarningRule entity = input.Adapt<WarningRule>();
		await _rep.InsertAsync(entity);
	}

	/// <summary>
	/// 删除预警规则
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/warningRule/delete")]
	public async Task Delete(DeleteWarningRuleInput input)
	{
		WarningRule entity = await _rep.GetFirstAsync((WarningRule u) => u.Id == input.Id);
		await _rep.DeleteAsync(entity);
	}

	/// <summary>
	/// 更新预警规则
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/warningRule/edit")]
	[OperationLog("更新预警规则", "name")]
	public async Task Update(UpdateWarningRuleInput input)
	{
		WarningRule entity = input.Adapt<WarningRule>();
		await _rep.UpdateAsync(entity);
	}

	/// <summary>
	/// 获取预警规则
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpGet("/warningRule/detail")]
	public async Task<WarningRule> Get([FromQuery] QueryeWarningRuleInput input)
	{
		return await _rep.GetFirstAsync((WarningRule u) => u.Id == input.Id);
	}

	/// <summary>
	/// 获取预警规则列表
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpGet("/warningRule/list")]
	public async Task<dynamic> List([FromQuery] WarningRuleInput input)
	{
		return await _rep.AsQueryable().ToListAsync();
	}
}
